/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if (head == nullptr)
        {
            return nullptr;
        }
        int len = 0;
        ListNode *cur = head;
        while (cur != nullptr)
        {
            ++len;
            cur = cur->next;
        }
        k = k % len;
        if (k == 0)
        {
            return head;
        }
        ListNode *fast = head;
        ListNode *slow = head;
        for (int i = 0; i < k; ++i)
        {
            fast = fast->next;
        }
        while (fast->next != nullptr)
        {
            fast = fast->next;
            slow = slow->next;
        }
        ListNode *newHead = slow->next;
        slow->next = nullptr;
        fast->next = head;
        return newHead;
    }
};